<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline" style="text-align: left">
      <el-form-item label="巡更路线名称">
        <el-input v-model="formInline.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getpatrolpathocate">查询</el-button>
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="addpatrolpath">新增</el-button>
      </el-form-item>
    </el-form>
    <br>
    <el-table border="" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection"> </el-table-column>
      <el-table-column prop="id" label="序号" align="center"> </el-table-column>
      <el-table-column prop="name" label="巡更路线名称" align="center">
      </el-table-column>
      <el-table-column prop="sum" label="巡更点数量" align="center">
      </el-table-column>
      <el-table-column prop="remark" label="巡更点位" align="center">
        <template slot-scope="scope">
          <!-- <div v-for="item in scope.row.patroName" v-bind:key="item.name">{{ item.name }}</div> -->
          <span v-for="item in scope.row.patroName" v-bind:key="item.name">{{
            item.name + "、"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-link type="warning" @click="updatepatrolpath(scope.row)">编辑</el-link>
          <el-link type="danger" @click="delpatrolpath(scope.row)">删除</el-link>
          <el-link type="primary" @click="addpatro(scope.row)">分配巡更点</el-link>
        </template>
      </el-table-column>
    </el-table>
    <br>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="formInline.pageIndex" :page-sizes="[10, 20, 30, 40]" :page-size="formInline.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="count">
    </el-pagination>

    <el-dialog title="新增" :visible.sync="dialogaddVisible" width="30%">
      <el-form :model="formData">
        <el-form-item label="巡更路线名称" :label-width="formLabelWidth">
          <el-input v-model="formData.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="formData.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="postpatrolpath">保存</el-button>
        <el-button type="primary" @click="dialogaddVisible = false">取消</el-button>
      </span>
    </el-dialog>

    <el-dialog title="‘编辑" :visible.sync="dialogputVisible" width="30%">
      <el-form :model="formData">
        <el-form-item label="巡更路线名称" :label-width="formLabelWidth">
          <el-input v-model="formData.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="formData.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="putpatrolpath">保存</el-button>
        <el-button type="primary" @click="dialogputVisible = false">取消</el-button>
      </span>
    </el-dialog>

    <el-dialog title="分配巡查点" :visible.sync="dialogTableVisible" width="30%">
      <el-checkbox-group v-model="pp.houseid">
        <el-checkbox v-for="item in value" v-bind:key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
      </el-checkbox-group>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="postpatroldetailapi">分配巡更点</el-button>
        <el-button type="primary" @click="dialogTableVisible = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getpatrolpathapi,
  postpatrolpathapi,
  putpatrolpathapi,
  deletepatrolpathapi,
} from "@/api/safeManage/Patrolpath";
import { getlistpatrolcollocateapi } from "@/api/safeManage/PatrolCollocate";
import {
  getidpatroldetailapi,
  postpatroldetailapi,
} from "@/api/safeManage/Patroldetail";
export default {
  data() {
    return {
      data: [],
      defaultProps: {},
      tableData: [],
      formInline: {
        pageIndex: 1,
        pageSize: 10,
        name: "",
      },
      value: [],
      count: 0,
      dialogaddVisible: false,
      dialogputVisible: false,
      dialogTableVisible: false,
      formLabelWidth: "120px",
      formData: {
        id: 0,
        name: "",
        sum: 0,
        remark: "",
      },
      options: [],
      pp: {
        houseid: [],
        lineid: 0,
      },
    };
  },
  methods: {
    getpatrolpathocate() {
      getpatrolpathapi(this.formInline).then((res) => {
        this.tableData = res.data.data;
        this.count = res.data.count;
      });
    },
    handleChange(value) {
      console.log(value);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.formInline.pageSize = val;
      this.getpatrolcollocate();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.formInline.pageIndex = val;
      this.getpatrolcollocate();
    },
    resetForm() {
      this.formInline.name = "";
    },
    handleSelectionChange(val) { },
    addpatrolpath() {
      (this.formData = {
        id: 0,
        name: "",
        sum: 0,
        remark: "",
      }),
        (this.dialogaddVisible = true);
    },
    postpatrolpath() {
      postpatrolpathapi(this.formData).then((res) => {
        if (res.data.message == "操作成功") {
          this.$message({
            message: "保存成功",
            type: "success",
          });
          this.getpatrolpathocate();
          this.dialogaddVisible = false;
        } else {
          this.$message({
            message: "保存失败",
            type: "error",
          });
        }
      });
    },
    updatepatrolpath(row) {
      this.dialogputVisible = true;
      this.formData = row;
    },
    putpatrolpath() {
      putpatrolpathapi(this.formData).then((res) => {
        if (res.data.message == "操作成功") {
          this.$message({
            message: "保存成功",
            type: "success",
          });
          this.getpatrolpathocate();
          this.dialogputVisible = false;
        } else {
          this.$message({
            message: "保存失败",
            type: "error",
          });
        }
      });
    },
    delpatrolpath(row) {
      if (confirm("记录数据删除将无法恢复，确认删除吗？")) {
        deletepatrolpathapi(row).then((res) => {
          if (res.data.message == "操作成功") {
            this.$message({
              message: "删除成功",
              type: "success",
            });
            this.getpatrolpathocate();
          } else {
            this.$message({
              message: "删除失败",
              type: "error",
            });
          }
        });
      }
    },
    addpatro(row) {
      this.dialogTableVisible = true;
      this.pp.lineid = row.id;
      getlistpatrolcollocateapi().then((res) => {
        this.value = res.data.data;
      });
      this.pp.houseid = [];
      getidpatroldetailapi(row).then((res) => {
        res.data.forEach((element) => {
          this.pp.houseid.push(element.houseId);
        });
      });
    },
    postpatroldetailapi() {
      postpatroldetailapi(this.pp).then((res) => {
        if (res.data == true) {
          this.$message({
            message: "分配成功",
            type: "success",
          });
          this.getpatrolpathocate();
          this.dialogTableVisible = false;
        } else {
          this.$message({
            message: "分配失败",
            type: "error",
          });
        }
      });
    },
  },
  created() {
    this.getpatrolpathocate();
  },
};
</script>
<style></style>